package com.view.jameson.androidrecyclerviewcard;

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
//import android.support.v4.widget.SwipeRefreshLayout;
//import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.view.jameson.library.BannerRecyclerView;
import com.view.jameson.library.BannerScaleHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * changed by 二精-霁雪清虹 on 2017/11/19
 */
public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {

    protected SwipeRefreshLayout mSwipeLayout;
    private BannerRecyclerView mRecyclerView;
    private List<CardViewBean> mList = new ArrayList<>();
    private BannerScaleHelper mBannerScaleHelper = null;
    private CardAdapter mAdapter;
    public static String flag = "";
    private ImageView im111;
    private ImageView im222;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        super.setContentView(R.layout.activity_main);
        initView();
        init();
    }

    private void init() {
//        for (int i = 0; i < 3; i++) {
//        mList.add(new CardViewBean(R.drawable.pic4));
//        mList.add(new CardViewBean(R.drawable.pic5));
//        mList.add(new CardViewBean(R.drawable.pic6));
//        mList.add(new CardViewBean(R.drawable.h1));
//        mList.add(new CardViewBean(R.drawable.h2));
//        mList.add(new CardViewBean(R.drawable.h3));
//        mList.add(new CardViewBean(R.drawable.h4));
//        mList.add(new CardViewBean(R.drawable.h5));
//        mList.add(new CardViewBean(R.drawable.h6));
//        mList.add(new CardViewBean(R.drawable.h7));
//        mList.add(new CardViewBean(R.drawable.h8));
//        mList.add(new CardViewBean(R.drawable.h9));
//        mList.add(new CardViewBean(R.drawable.h10));
//        mList.add(new CardViewBean(R.drawable.h11));
//        }

        mList.add(new CardViewBean("https://m.bbmaiqiu.com/api/forehead/system/file/get/65812fed0419d3282052e1ba.jpeg"));
        mList.add(new CardViewBean("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png"));
        mList.add(new CardViewBean("https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png"));
//        mList.add(new CardViewBean("https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"));
        Glide.with(this)
                .load("https://m.bbmaiqiu.com/api/forehead/system/file/get/65812fed0419d3282052e1ba.jpeg")
//                    .placeholder(R.mipmap.ic_launcher)
                .into(im111);

        Glide.with(this)
                .load("https://m.bbmaiqiu.com/api/forehead/system/file/get/65812fed0419d3282052e1ba.jpeg")
//                    .placeholder(R.mipmap.ic_launcher)
                .into(im222);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new CardAdapter(mList,this);
        mRecyclerView.setAdapter(mAdapter);
//        mRecyclerView.addItemDecoration(new CenterItemDecoration());

        // mRecyclerView绑定scale效果
        mBannerScaleHelper = new BannerScaleHelper();
        if (mList.size() <= 3) {
            mBannerScaleHelper.setFirstItemPos(1);
        } else {
            mBannerScaleHelper.setFirstItemPos(mList.size() * 10000);
        }

        mBannerScaleHelper.attachToRecyclerView(mRecyclerView);
//        PagerSnapHelper pagerSnapHelper=new PagerSnapHelper();
//        pagerSnapHelper.attachToRecyclerView(mRecyclerView);
//        ViewGroup.LayoutParams layoutParams=  mRecyclerView.getLayoutParams();
//        if (layoutParams instanceof SwipeRefreshLayout.LayoutParams) {
//            // 将布局参数转换为 LinearLayout.LayoutParams 类型
//            SwipeRefreshLayout.LayoutParams lp = (SwipeRefreshLayout.LayoutParams) layoutParams;
//            // 设置布局参数为水平居中
//            lp.gravity = Gravity.CENTER_HORIZONTAL;
//            // 重新设置布局参数
//            mRecyclerView.setLayoutParams(lp);
//        }

//        mRecyclerView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                //Log.e("TAG", "postDelayed scrollToPosition" );
//                int position = 0;
//                if (mList.size() == 2 || mList.size() == 3) {
//                    position = position + 1;
//                } else if (mList.size() > 3) {
////                    position = (position % mList.size()) * 10000;
//                    position = mList.size() * 10000 + position;
//                    if (position == 0) {
//                        position = (mList.size()) * 10000;
//                    }
//                }
//
//                Log.e("positionposition", position + "==");
//                mBannerScaleHelper.scrollToPosition(position);
//            }
//        }, 1000);

        mRecyclerView.setOnPageChangeListener(new BannerRecyclerView.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                if (mList.size() <= 3) {
                    position = position - 1;
                    if (position == -1) {
                        position = 0;
                    }
                }
                Log.e("当前位置", position + "==");
            }
        });
    }

    private void initView() {
        mRecyclerView = (BannerRecyclerView) findViewById(R.id.recyclerView);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
        mSwipeLayout.setEnabled(false);
        im111=findViewById(R.id.im111);
        im222=findViewById(R.id.im222);
        //为SwipeRefreshLayout设置监听事件
        mSwipeLayout.setOnRefreshListener(this);
        //为SwipeRefreshLayout设置刷新时的颜色变化，最多可以设置4种
        mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
    }

    @Override
    public void onRefresh() {
        //刷新
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //结束后停止刷新
                mSwipeLayout.setRefreshing(false);
                mList.clear();
//                for (int i = 0; i < 2; i++) {
//                    mList.add(R.drawable.pic6);
//                    mList.add(R.drawable.pic4);
//                }

                //如果需要带缩放的scale需要调用一下，否则缩放效果会出现缩放误差
                //mRecyclerView.smoothScrollToPosition(mBannerScaleHelper.getCurrentItem());
                mBannerScaleHelper.setCurrentItem(mBannerScaleHelper.getCurrentItem(), true);

                mAdapter.setList(mList);
                mAdapter.notifyDataSetChanged();
            }
        }, 1500);
    }
}
